﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Collections;

namespace Phong_CTSV
{
    public class ExportReport
    {
        //Form form = null;
        //String formName = null;
        long uni = 0;
        List<Columns_Header> columnsHeader = null;
        Data_Table[,] dataTable = null;
        Data_Table[] Sum = null;
        int numColumn = 0;
        int numRow = 0;
        QuerrySQL query = new QuerrySQL();
        Table tableExport = new Table();
        string uniName;
        List<String> uniList = new List<String>();
        String titleForm;
        public ExportReport(Data_Table[,] data, Data_Table[] Sum, string uni_Name, List<Columns_Header> ch,String title)
        {
            University u = query.Select_UniversityByUser(uni_Name);
            uni = u.ID;
            uniName = u.Name;
            titleForm = title;
            columnsHeader = new List<Columns_Header>(ch);
            getUniList();
            foreach (Columns_Header h in columnsHeader)
                if (!h.Has_child)
                    numColumn++;
            numRow = uniList.Count;
            if (uni == 1)
            {
                numRow = uniList.Count - 1;
                uniList.RemoveAt(0);
            }
            getDataTable(data);
            getSum(Sum);
 
            
        }
        #region getValue
        private void getDataTable(Data_Table[,] data)
        {
            try
            {
                dataTable = new Data_Table[numRow, numColumn];

                for (int i = 0; i < numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {
                        dataTable[j, i] = data[j, i];
                    }
                }
            }
            catch { }
        }
        
        private void getSum(Data_Table[] sum)
        {
            Sum = new Data_Table[numColumn];
            for (int i = 0; i < numColumn; i++)
                Sum[i] = sum[i];
        }
        private void getUniList()
        {
            if (uni == 1)
            {
                List<University> temp = query.Select_AllUniversity();
                foreach (University u in temp)
                    uniList.Add(u.Name);
            }
            else
                uniList.Add(uniName);

        }
        
        #endregion
        public List<Columns_Header> ColumnsHeaderList
        {
            get
            {
                return columnsHeader;
            }
        }
       
       
        #region Export file
        private void createHeaderExport()
        {
            TableRow rowHeader1 = new TableRow();
            TableCell cellHeader1 = new TableCell();
            int numberCellMerge = numColumn+1;
            
            cellHeader1.RowSpan = 1;
            cellHeader1.ColumnSpan = numberCellMerge;
            cellHeader1.Text = "Cộng hòa xã hội chủ nghĩa Việt Nam";
            cellHeader1.Font.Bold = true;
            cellHeader1.HorizontalAlign = HorizontalAlign.Center;
            rowHeader1.Cells.Add(cellHeader1);
            tableExport.Rows.Add(rowHeader1);

            TableRow rowHeader2 = new TableRow();
            TableCell cellHeader2 = new TableCell();
            cellHeader2.RowSpan = 1;
            cellHeader2.ColumnSpan = numberCellMerge;
            cellHeader2.Text = "Độc lập- Tự do- Hạnh phúc";
            cellHeader2.Font.Bold = true;
            cellHeader2.HorizontalAlign = HorizontalAlign.Center;
            rowHeader2.Cells.Add(cellHeader2);
            tableExport.Rows.Add(rowHeader2);

            TableRow rowHeader3 = new TableRow();
            TableCell cellHeader3 = new TableCell();
            cellHeader3.RowSpan = 1;
            cellHeader3.ColumnSpan = numberCellMerge;
            cellHeader3.Text = "Trường: " + uniName;
            cellHeader3.Font.Bold = true;
            cellHeader3.HorizontalAlign = HorizontalAlign.Left;
            rowHeader3.Cells.Add(cellHeader3);
            tableExport.Rows.Add(rowHeader3);

            TableRow rowHeader4 = new TableRow();
            TableCell cellHeader4 = new TableCell();
            cellHeader4.RowSpan = 1;
            cellHeader4.ColumnSpan = numberCellMerge;
            cellHeader4.Text = "Năm: " + DateTime.Now.Year.ToString();
            cellHeader4.Font.Bold = true;
            cellHeader4.HorizontalAlign = HorizontalAlign.Left;
            rowHeader4.Cells.Add(cellHeader4);
            tableExport.Rows.Add(rowHeader4);

            TableRow rowHeader5 = new TableRow();
            TableCell cellHeader5 = new TableCell();
            cellHeader5.RowSpan = 1;
            cellHeader5.ColumnSpan = numberCellMerge;
            cellHeader5.Text = titleForm;
            cellHeader5.Font.Bold = true;
            cellHeader5.Font.Size = 15;
            cellHeader5.HorizontalAlign = HorizontalAlign.Center;
            rowHeader5.Cells.Add(cellHeader5);
            tableExport.Rows.Add(rowHeader5);
        }
        private void createColumnHeader()
        {
            TableRow rowColumnHeader = new TableRow();
            TableRow rowColumnHeader_Child = new TableRow();

            bool row2Header = false;
            //---------- Column TRUONG----
            TableCell cellFirst = new TableCell();
            cellFirst.RowSpan = 2;
            cellFirst.ColumnSpan = 1;
            cellFirst.Text = "Trường";
            cellFirst.Font.Bold = true;
            cellFirst.HorizontalAlign = HorizontalAlign.Center;
            rowColumnHeader.Cells.Add(cellFirst);

            List<Columns_Header> column_Header_Child = new List<Columns_Header>();
            foreach (Phong_CTSV.Columns_Header ch in ColumnsHeaderList)
            {

                column_Header_Child.Clear();
                if (ch.Parent_id == 0)
                {
                    int child = 0;
                    foreach (Phong_CTSV.Columns_Header childHeader in ColumnsHeaderList)
                    {
                        if (childHeader.Parent_id == ch.ID)
                        {
                            child++;
                            column_Header_Child.Add(childHeader);

                        }
                    }
                    if (child == 0)
                    {
                        TableCell cellColumnHeader = new TableCell();
                        cellColumnHeader.RowSpan = 2;
                        cellColumnHeader.ColumnSpan = 1;
                        cellColumnHeader.Text = ch.Name;
                        cellColumnHeader.Font.Bold = true;
                        cellColumnHeader.HorizontalAlign = HorizontalAlign.Center;
                        rowColumnHeader.Cells.Add(cellColumnHeader);
                    }
                    else
                    {
                        TableCell cellColumnHeader = new TableCell();
                        cellColumnHeader.RowSpan = 1;
                        cellColumnHeader.ColumnSpan = child;
                        cellColumnHeader.Text = ch.Name;
                        cellColumnHeader.Font.Bold = true;
                        cellColumnHeader.HorizontalAlign = HorizontalAlign.Center;
                        rowColumnHeader.Cells.Add(cellColumnHeader);
                        foreach (Columns_Header child_Hader in column_Header_Child)
                        {
                            TableCell cellColumnHeader_Child = new TableCell();
                            cellColumnHeader_Child.RowSpan = 1;
                            cellColumnHeader_Child.ColumnSpan = 1;
                            cellColumnHeader.Font.Bold = true;
                            cellColumnHeader_Child.Text = child_Hader.Name;
                            cellColumnHeader_Child.HorizontalAlign = HorizontalAlign.Center;
                            rowColumnHeader_Child.Cells.Add(cellColumnHeader_Child);
                        }
                        row2Header = true;
                    }

                }
            }
            tableExport.Rows.Add(rowColumnHeader);
            if (row2Header)
                tableExport.Rows.Add(rowColumnHeader_Child);
            else
            {
                TableRow rowTemp = new TableRow();
                tableExport.Rows.Add(rowTemp);
            }
        }
        
       private void createData_NoRow()
        {
            int rowIndex = 0;
            
            for (int j = 0; j < numRow; j++)
            {
                TableRow row = new TableRow();
                TableCell cell_First = new TableCell();
                cell_First.RowSpan = 1;
                cell_First.ColumnSpan = 1;
                cell_First.Text = uniList[j];
                cell_First.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell_First);
                for (int i = 0; i < numColumn; i++)
                {
                    TableCell cell_Data = new TableCell();
                    cell_Data.RowSpan = 1;
                    cell_Data.ColumnSpan = 1;
                    cell_Data.Text = dataTable[rowIndex, i].Value.ToString();
                    cell_Data.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell_Data);

                }
                rowIndex++;
                tableExport.Rows.Add(row);
            }

        }
        private void addTotalRow()
        {

            TableRow row = new TableRow();
            TableRow row1 = new TableRow();// tach hang tinh tong ra rieng
            TableCell cell_First = new TableCell();
            cell_First.RowSpan = 1;
            cell_First.ColumnSpan = 1;
            cell_First.Text = "Tổng:";
            cell_First.Font.Bold = true;
            cell_First.HorizontalAlign = HorizontalAlign.Center;
            row.Cells.Add(cell_First);
            for (int i = 0; i < numColumn; i++)
            {
                TableCell cell_Data = new TableCell();
                cell_Data.RowSpan = 1;
                cell_Data.ColumnSpan = 1;
                cell_Data.Text = Sum[i].Value;
                cell_Data.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell_Data);

            }
                tableExport.Rows.Add(row);
            
        }
        private void createEndFile()
        {
            TableRow row = new TableRow();
            int numberCellMerge = numColumn+1;
            TableRow row1 = new TableRow();
            TableRow row2 = new TableRow();
            tableExport.Rows.Add(row1);
            tableExport.Rows.Add(row2);
            TableRow rowEnd = new TableRow();
            TableCell cell_End1 = new TableCell();
            cell_End1.RowSpan = 2;
            cell_End1.ColumnSpan = numberCellMerge / 2;
            cell_End1.Text = "Người lập bảng";
            cell_End1.HorizontalAlign = HorizontalAlign.Center;
            rowEnd.Cells.Add(cell_End1);
            TableCell cell_End2 = new TableCell();
            cell_End2.RowSpan = 2;
            cell_End2.ColumnSpan = numberCellMerge / 2;
            cell_End2.Text = "TP HCM, Ngày   Tháng    Năm    <br/>Người lập bảng";
            cell_End2.HorizontalAlign = HorizontalAlign.Center;
            rowEnd.Cells.Add(cell_End2);
            tableExport.Rows.Add(rowEnd);
        }

        public string CreateExcelFile()
        {
            createHeaderExport();
            createColumnHeader();
            createData_NoRow();
            addTotalRow();
            createEndFile();

            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.Html32TextWriter html = new System.Web.UI.Html32TextWriter(writer);
            tableExport.RenderControl(html);
            String output = writer.ToString();
            return output;

        }

        #endregion

    }
}